__bridge 和 Core Foundation
2017-03-14 10:25:36 # Objective-C

这篇知识比较偏,是偏低层一些的。

一:__birdge

由于__bridge很少接触,所以专门去查找了一下。其实它的作用很简单,就是:实现id类型与void* 类型的相互转换。

看下代码:

1
2
3
4
id obj = [[NSObject alloc]init];
void *c = (__bridge void *)(obj);
id d = (__bridge id)(c);
NSLog(@"\n obj=%@,\n c=%@,\n d=%@\n",obj,c,d);

这里我们就用到了__bridge
打印结果如下:

__bridgePrint

下面看下使用和不使用__bridge的对比

__bridgeError

__bridge


二:Core Foundation

1、什么是Core Foundation

Core Foundation框架 (CoreFoundation.framework) 是一组C语言接口,它们为iOS应用程序提供基本数据管理和服务功能。

注:Core Foundation是底层框架,它的对象并不在ARC管理下,所以需要开发者自己去维护这些对象的引用计数。

2、Core Foundaton 的作用
至于它的作用,这里有一段英文解释:

The programming interfaces of Core Foundation objects have been designed for ease of use and reuse. At a general level, Core Foundation:
Enables sharing of code and data among various frameworks and libraries.
Makes some degree of operating-system independence possible .
Supports internationalization with Unicode strings.
Provides common API and other useful capabilities, including a plug-in architecture, XML property lists, and preferences.
Core Foundation makes it possible for the different frameworks and libraries on OS X to share code and data. Applications, libraries, and frameworks can define C routines that incorporate Core Foundation types in their external interfaces; they can thus communicate data—as Core Foundation objects—to each other through these interfaces.

翻译后,大致意思是这样的:

Core Foundation对象的编程接口已设计为易于使用和重用。 在一般的意义,Core Foundation
1、支持在各种框架和库之间共享代码和数据
2、使某种程度的操作系统独立性成为可能
3、支持使用Unicode字符串进行国际化
4、提供通用API和其他有用的功能,包括插件架构,XML属性列表和首选项
Core Foundation使得OS X上的不同框架和库可以共享代码和数据。 应用程序,库和框架可以定义在其外部接口中并入Core Foundation类型的C例程; 它们因此可以通过这些接口将数据作为Core Foundation对象传递到彼此。

三、Objective-C 和 Core Foundation 对象之间转换

iOS允许Objective-CCore Foundation 对象之间可以轻松的转换。

在《iOS开发进阶》一书中,唐巧提到:在ARC下,有时候需要将一个Core Foundation对象转换成一个Object-C对象,这个时候我们需要告诉编译器,转换过程中引用计数需要如何调整,这时也会用到bridge这个关键字。

  • __bridge: 只做类型转换,不修改相关对象的引用计数,原来的Core Foundation对象在不用时,需要调用CFRelease方法。
  • __bridge_retained: 类型转换后将相关对象的引用计数加1,原来的Core Foundation对象在不用时,需要调用CFRelease方法。
  • __bridge_transfer: 类型转换后,将该对象的引用计数交给ARC管理,Core Foundation对象在不用时,不再需要调用CFRelease方法。

__bridge:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    id obj = [[NSObject alloc]init];
// void *a = obj;
// id b = a;
void *c = (__bridge void *)(obj);//只做类型转换,不修改相关对象的引用计数
NSLog(@"obj-c count is %ld", CFGetRetainCount(c));//输出结果: obj-c count is 1
id d = (__bridge id)(c);
NSLog(@"obj-d retainCount %ld",CFGetRetainCount((__bridge CFTypeRef)(d)));//输出结果:obj-d retainCount 2
NSLog(@"\n obj=%@,\n c=%@,\n d=%@\n",obj,c,d);
//__bridge
CFStringRef CFString = CFStringCreateWithCString(kCFAllocatorDefault, "hello Core Foundation", kCFStringEncodingASCII);
NSLog(@"CFString retainCount= %ld",CFGetRetainCount(CFString));//CFString retainCount= 1
NSString *string = (__bridge NSString *)CFString;
NSLog(@"CFstring==%@,\nstring==%@\n",CFString,string);
NSString *qStr = @"qgh";
CFStringRef qCFStr = (__bridge CFStringRef)qStr;
NSLog(@"qStr==%@,qCFStr==%@\n",qStr,qCFStr);
NSArray *array = @[@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h"];
CFArrayRef CFArray = (__bridge CFArrayRef)(array);
NSLog(@"CFArray retainCount= %ld",CFGetRetainCount(CFArray));//CFArray retainCount= 1
NSLog(@"array==%@",CFArray);

__bridge_transfer:

1
2
3
4
5
6
//__bridge_transfer:类型转换后,将该对象的引用计数交给ARC管理.
NSString *transferString = [[NSString alloc]initWithFormat:@"test:::__bridge_transfer"];
CFStringRef CFTransferString = (__bridge_retained CFStringRef)(transferString);
NSLog(@"CFTransferString count is %ld", CFGetRetainCount(CFTransferString));//CFTransferString count is 2
transferString = (__bridge_transfer NSString *)(CFTransferString);
NSLog(@"transferString count is %ld", CFGetRetainCount((__bridge CFTypeRef)(transferString)));//transferString count is 1

__bridge_retained:

1
2
3
4
5
6
//__bridge_retained: 类型被转换时,其对象的所有权也将被变换后变量所持有
void *y = 0;
id object = [[NSObject alloc]init];
y = (__bridge_retained void*)(object);//类型转换后将相关对象的引用计数加1
NSLog(@"object-y count is %ld", CFGetRetainCount(y));//object-y count is 2
NSLog(@"class=%@",[(__bridge id)(y) class]);